import {take,call,put,select} from 'redux-saga/effects'
import {get, post, put_http} from '../axios'
import {actionsTypes as IndexActionTypes} from '../reducers'
import {actionsTypes as EActionTypes} from '../reducers/evaluate'
import history from '../history';

export function* get_columns () {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(get, `/mem/project-evaluate-columns`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* getColumnsFlow () {
    while (true){
        let req = yield take(EActionTypes.GET_COLUMNS);
        let res = yield call(get_columns);
        if (res&&res.success) {
            yield put({type: EActionTypes.RESPONSE_COLUMNS, data: res.data})
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

export function* submit_comment (data, project_id) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(post, `/mem/project-evaluate/${project_id}`, data);
    } catch (err) {
        console.log(err)
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* submitCommentFlow () {
    while (true){
        let req = yield take(EActionTypes.SUBMIT_COMMENT);
        let res = yield call(submit_comment, req.data, req.project_id);
        if (res&&res.success) {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '评价成功', msgType: 'success'});
            yield put({type: EActionTypes.CHANGE_REFRESH, data: true})
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

export function* get_comment (project_id, role) {
    yield put({type: IndexActionTypes.FETCH_START});
    try {
        return yield call(get, `/mem/project-evaluate/${project_id}/${role}`);
    } catch (err) {
        yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: '网络请求错误', msgType: 'fail'});
    } finally {
        yield put({type: IndexActionTypes.FETCH_END})
    }
}

export function* getCommentFlow () {
    while (true){
        let req = yield take(EActionTypes.GET_COMMENT);
        let res = yield call(get_comment, req.project_id, req.role);
        if (res&&res.success) {
            yield put({type: EActionTypes.RESPONSE_COMMENT, data: res.data})
        } else {
            yield put({type: IndexActionTypes.SET_MESSAGE, msgContent: res&&res.msg, msgType: 'fail'});
        }
    }
}

export function* changeRefreshFlow () {
    while (true){
        let req = yield take(EActionTypes.CHANGE_REFRESH);
        yield put({type: EActionTypes.CHANGE_REFRESH, data: req.status})
    }
}